<?php

class Ticket extends Controller {

	function setVars(){
		$this->tpl['jsOnLoad']  = '';
		$this->tpl['head'] = '';
		$this->tpl['title'] = 'Help Desk';
		$this->uid = $this->session->userdata('uid');
	}
	
	
	function Ticket()
	{
		parent::Controller();	
		log_message('debug', "Ticket: Controler Class Initialized");
		$this->setVars();
		//$this->output->enable_profiler(TRUE);
	}

	function index()
	{
		$this->auth->CheckTaMeR();
		$query = $this->db->getwhere('Ticket', array('people_id' => $this->uid));
		if ($query->num_rows() > 0)
		{
			foreach($query->result_array() as $k => $s):
				$tbresult[$k] = $s;
			endforeach;
			$tbresult = delids($tbresult);
			$this->load->library('table');
			$this->table->set_heading(array_keys($tbresult[0]));
			foreach($tbresult as $row):
			$v = $row['Ticket No'];
			$row['Ticket No'] = anchor('ticket/view/'.$v,$v);
				$this->table->add_row($row);
			endforeach;			
			$this->tpl['body'] = $this->table->generate();
			$this->table->clear();

		}
		$this->load->view('layout/blank', $this->tpl);
	}
	
	
	function newTicket()
	{
		if(isset($_POST['NewTicket'])){
			$this->load->library('validation');
			$rules['Subject']="trim|required|min_length[4]|max_length[80]|xss_clean";
			$rules['Body']  ="trim|required|min_length[10]|max_length[2000]|xss_clean";
			$this->validation->set_rules($rules);
			if($this->validation->run() == FALSE)
			{
				#We got to get the Messanger working here again
				$this->newTicketView();
			}
			else
			{
				$this->load->helper('string');
				$no = random_string('alnum', 10);
				$alias = $this->session->userdata('Alias');
				$data = array(
					'uid'			=> $this->uid,
					'Ticket No'	=> $no,
					'Alias'		=> $alias,
					'wsid'		=> '4',
					'Category'	=> $this->input->post('Category'),
               'Subject' 	=> $this->validation->Subject,
               'State' 		=> 'New'
            );
				$this->db->insert('Ticket', $data);
				$id = $this->db->insert_id();
				$data = array(
					'tid'			=> $id,
					'Alias'		=> $alias,
					'Priority'	=> $this->input->post('Priority'),
               'Message'	=> $this->validation->Body
            );
				$this->db->insert('Ticket_Message', $data);
				redirect('/ticket/view/'.$no, 'refresh');

			}
		}else{
			$this->newTicketView();
		}
	}

	function newTicketView()
	{
		$this->auth->CheckTaMeR();
		$this->load->model('pdb');
		$Priority = $this->pdb->dbGet('select', 'Priority', 'Posts');
		$Category = $this->pdb->dbGet('select', 'Category', 'Ticket');
		
		$o = '<select name="Priority">';
		foreach($Priority as $k => $v){
			$o .= "<option value=\"$v->value\"";
			if($v->value == 'Medium'){
				$o .= ' selected';
			}
			$o .= ">$v->value</option>\n";
		}
		$o .= '</select>';
		$this->tpl['Priority'] = $o;

		$o = '<select name="Category">';
		foreach($Category as $k => $v){
			$o .= "<option value=\"$v->value\">$v->value</option>\n";
		}
		$o .= '</select>';
		$this->tpl['Category'] = $o;
		unset($o);
		$this->load->view('ticket/new', $this->tpl);
	}
	
	function view()
	{
		$this->auth->CheckTaMeR();
		$tno = $this->uri->segment(3, 0);
		if($tno == 0)
		{
			redirect('/contact/login', 'location');
		}
		elseif($this->uid < 1)
		{
			redirect('/contact/login', 'location');
		}
		else
		{
			$ticketViewHead = array (
				'table_open'          => '<table border="1" cellpadding="2" cellspacing="0" width="90%" align="center"><thead>',
				
				'heading_row_start'   => '<tr>',
				'heading_row_end'     => '</tr>',
				'heading_cell_start'  => '<th>',
				'heading_cell_end'    => '</th>',
				
				'row_start'           => '<tr class="odd">',
				'row_end'             => '</tr>',
				'cell_start'          => '<td>',
				'cell_end'            => '</td>',
				
				'row_alt_start'       => '<tr class="even">',
				'row_alt_end'         => '</tr>',
				'cell_alt_start'      => '<td colspan="3">',
				'cell_alt_end'        => '</td>',
				
				'table_close'         => '</thead></table>'
				);
			$ticketViewBody = array (
				'table_open'          => '<table border="1" cellpadding="2" cellspacing="0" width="90%" align="center"><tbody>',
				'heading_row_start'   => '<tr>',
				'heading_row_end'     => '</tr>',
				'heading_cell_start'  => '<th colspan="2">',
				'heading_cell_end'    => '</th>',
				
				'row_start'           => '<tr class="odd">',
				'row_end'             => '</tr>',
				'cell_start'          => '<td  colspan="2">',
				'cell_end'            => '</td>',
				
				'row_alt_start'       => '<tr class="even">',
				'row_alt_end'         => '</tr>',
				'cell_alt_start'      => '<td colspan="6">',
				'cell_alt_end'        => '</td>',
				
				'table_close'         => '</tbody></table>'
				);
			$sql = "SELECT * FROM `Ticket` WHERE `Ticket No` = '$tno'";
			$dbticket = $this->db->query($sql);
			if ($dbticket->num_rows() > 0)
			{
				$tbticketrow = $dbticket->row_array();
				$dbticket->free_result();
			}
			//Get the id so we can load the right messages form the Message table
			$tid = $tbticketrow['id'];
			
			// Remove all id fields
			$tbticketrow = delids($tbticketrow);
			// View Ticket table
			$this->load->library('table');
			$this->table->set_template($ticketViewHead);
			$this->table->set_heading(array_keys($tbticketrow));
			$this->table->add_row($tbticketrow);
			$this->tpl['tbticket'] = $this->table->generate();
			$this->table->clear();
			
			// Get data from Ticket Messages
			$sql = "SELECT * FROM Ticket_Message";
			$dbquery4 = $this->db->query($sql);
			foreach($dbquery4->result_array() as $k => $s):
			$query5[$k] = $s;
			endforeach;
			// Remove id fields
			$query5 = delids($query5);
			
			foreach($query5 as $k=>$v){
				$query5[$k]['Date'] = $this->timetamer->ldate($query5[$k]['Date']);
			}
			
			// Restructure db table to view massages in second row.
			foreach($query5 as $k => $s):
			$query3 = $query5[$k];
			unset($query3['Message']);
			$query6[] = $query3;
			$query6[]['Message'] = $query5[$k]['Message'];
			endforeach;
			
			$this->table->set_template($ticketViewBody);
			$this->table->set_heading(array_keys($query3));
			unset($query3);
			foreach($query6 as $row):
			$this->table->add_row($row);
			endforeach;
			$this->tpl['tbtktmsg'] = $this->table->generate();
			$this->table->clear();
			
			$this->load->view('ticket/view', $this->tpl);
		}
	}
}
?>